package com.imagecollector.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.imagecollector.common.R;
import com.imagecollector.common.utils.AliOSSUtils;
import com.imagecollector.common.utils.DateTimeUtils;
import com.imagecollector.domain.dto.TaskAddDTO;
import com.imagecollector.domain.dto.TaskSearchDTO;
import com.imagecollector.domain.dto.TaskUpdateDTO;
import com.imagecollector.domain.entity.*;
import com.imagecollector.domain.dto.PictureDTO;
import com.imagecollector.domain.vo.TaskSearchVO;
import com.imagecollector.domain.vo.TaskVo;
import com.imagecollector.service.ITaskService;
import com.imagecollector.service.impl.TaskAbandonRecordServiceImpl;
import com.imagecollector.service.impl.TaskReceiverRecordServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.time.LocalDateTime;

/**
 * <p>
 * 任务表 前端控制器
 * </p>
 *
 * @author wrf
 * @since 2024-07-31
 */
@RestController
@RequestMapping("admin/task")
@Slf4j
@Api(tags = "web端任务相关接口")
public class TaskController {
    @Autowired
    private ITaskService taskService;
    @Autowired
    private TaskAbandonRecordServiceImpl taskAbandonRecordService;
    @Autowired
    TaskReceiverRecordServiceImpl taskReceiverRecordService;

    @ApiOperation("创建任务")
    @PostMapping("createTask")
    public R createTask(@RequestBody @Valid TaskAddDTO taskAddDTO) {
        taskService.createTask(taskAddDTO);
        return R.success().msg("创建成功");
    }

    @ApiOperation("上架/下架任务")
    @PostMapping("publishOrRemovalTask")
    public R publishTask(@RequestParam("taskId") @NotNull Integer taskId) {
        taskService.publishOrRemovalTask(taskId);
        return R.success().msg("操作成功");
    }

    @ApiOperation("修改任务信息")
    @PostMapping("updateTask")
    public R updateTask(@RequestBody @Valid TaskUpdateDTO taskUpdateDTO) {
        taskService.updateTask(taskUpdateDTO);
        return R.success().msg("修改成功");
    }

    @ApiOperation("查询任务详情")
    @GetMapping("getTaskInfo")
    public R getTaskInfo(@RequestParam("taskId") @NotNull Integer taskId) {
        TaskSearchVO taskInfo = taskService.getTaskInfo(taskId);
        return R.success().data("data", taskInfo);
    }

    @ApiOperation("取消用户接取")
    @PostMapping("cancelUserReceive")
    public R cancelUserReceive(@RequestParam("taskId") @NotNull Integer taskId,
                               @RequestParam("userId") @NotNull Integer userId) {
        taskService.cancelUserReceive(taskId, userId);
        return R.success().msg("取消成功");
    }

    @ApiOperation("任务列表分页查询")
    @PostMapping("taskList")
    public R taskList(@RequestBody TaskSearchDTO taskSearchDTO) {
        Page<TaskSearchVO> res = taskService.taskList(taskSearchDTO);
        return R.success().data("data", res.getRecords()).data("count", res.getTotal());
    }

}
